class TableField:
    def __init__(self):
        self.name = None
        self.jdbcType = None
        self.javaType = None
        self.javaName = None
        self.isPrimary = False
        self.remark = None

    def build(self, r):
        self.name = r[0]
        self.jdbcType = r[1]
        self.isPrimary = r[4] == 'PRI'
        self.remark = r[8]
        self.buildJavaName()
        self.buildJavaType()
        return self

    def buildJavaName(self):
        arr = self.name.split('_')
        sb = arr[0]
        for i in range(1, len(arr)):
            sb += self.upFirst(arr[i])
        self.javaName = sb

    def upFirst(self, name):
        if len(name) > 1:
            return str(name[0:1]).upper() + name[1:]
        return name

    def buildJavaType(self):
        if 'bigint' in str(self.jdbcType).lower():
            self.javaType = 'Long'
        elif 'char' in str(self.jdbcType).lower():
            self.javaType = 'String'
        elif 'int' in str(self.jdbcType).lower():
            self.javaType = 'Integer'
        elif 'decimal' in str(self.jdbcType).lower():
            self.javaType ='BigDecimal'
        elif 'longtext' in str(self.jdbcType).lower():
            self.javaType = 'String'
        elif 'datetime' in str(self.jdbcType).lower():
            self.javaType = 'Date'
        else:
            raise Exception("not support Type:" + self.jdbcType)

    def to_map(self):
        return {
            'name': self.name,
            'javaName': self.javaName,
            'jdbcType': self.jdbcType,
            'javaType': self.javaType,
            'isPrimary': self.isPrimary,
            'remark': self.remark
        }


class Table:

    def __init__(self, tfs):
        self.tableName = None
        self.tableComment = None
        self.tfs = tfs

    def build(self, r):
        self.tableName = r[0][0]
        self.tableComment = ''
        try:
            self.tableComment = str(r[0][1]).lower().split('engine')[1].split('comment=')[1]
        except Exception as e:
            pass
